import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_ume/flutter_ume.dart';
import 'package:flutter_ume_kit_dio/flutter_ume_kit_dio.dart';
import 'package:flutter_ume_kit_perf/flutter_ume_kit_perf.dart';
import 'package:flutter_ume_kit_ui/flutter_ume_kit_ui.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:tiktokadult/hj_page/home_business/search_page/widget/search_app_bar.dart';
import 'package:tiktokadult/hj_page/main_page/provider/ai_bubble_provider.dart';
import 'package:tiktokadult/hj_page/main_page/provider/bottom_bar_provider.dart';
import 'package:tiktokadult/hj_page/main_page/provider/module_provider.dart';
import 'package:tiktokadult/hj_page/mine_business/ai/mode/ai_gril_resp.dart';
import 'package:tiktokadult/hj_page/pre_sale/limit_time_event/limit_time_event_provider.dart';
import 'package:tiktokadult/hj_page/pre_sale/pre_sale_provider.dart';
import 'package:tiktokadult/hj_page/splash/splash_page.dart';
import 'package:tiktokadult/hj_utils/flutter_base.dart';
import 'package:tiktokadult/hj_utils/http_manager.dart';
import 'package:tiktokadult/hj_utils/screen.dart';
import 'package:tiktokadult/routers/router_map.dart';
import 'package:tiktokadult/tools_base/debug_log.dart';
import 'package:tiktokadult/tools_base/global_store/store.dart';
import 'package:tiktokadult/tools_base/video_download/video_download_manager.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import 'assets_tool/app_colors.dart';
import 'hj_page/main_page/provider/msg_provider.dart';

final RouteObserver<ModalRoute<void>> routeObserver = RouteObserver<ModalRoute<void>>();

void main() async {
  runZonedGuarded(() async {
    WidgetsFlutterBinding.ensureInitialized();
    VideoDownloadManager.instance.m3u8LoaderInit();
    await globalStore.init(); //获取密码
    httpManager.initDefault();

    const app = MyApp();
    if (!kReleaseMode) {
      PluginManager.instance
        ..register(DioInspector(dio: httpManager.dio))
        ..register(Performance())
        ..register(const WidgetInfoInspector())
        ..register(const WidgetDetailInspector());
      runApp(const UMEWidget(child: app));
    } else {
      runApp(app);
    }
  }, (exception, stackTrace) async {
    debugLog('exception = $exception\n stackTrace = $stackTrace');
  });
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    WakelockPlus.enable();
    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    screen.configData(context);

    PaintingBinding.instance.imageCache.maximumSize = 1000;
    PaintingBinding.instance.imageCache.maximumSizeBytes = 500 << 20;

    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        // 触摸收起键盘
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: ScreenUtilInit(
        designSize: const Size(428, 927),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: (context, child) {
          return MultiProvider(
            providers: [
              ChangeNotifierProvider<PreSaleProvider>.value(value: PreSaleProvider()), //预售
              ChangeNotifierProvider<GlobalStore>.value(value: globalStore),
              ChangeNotifierProvider.value(value: HomeBottomProvider()),
              ChangeNotifierProvider.value(value: DarkwebBottomProvider()),
              ChangeNotifierProvider.value(value: CommunityBottomProvider()),
              ChangeNotifierProvider.value(value: CartoonBottomProvider()),
              ChangeNotifierProvider.value(value: HomeModuleProvider()),
              ChangeNotifierProvider.value(value: CartoonModuleProvider()),
              ChangeNotifierProvider.value(value: CommunityModuleProvider()),
              ChangeNotifierProvider.value(value: DarkWebModuleProvider()),
              ChangeNotifierProvider.value(value: aibubbleProvider),
              ChangeNotifierProvider.value(value: AiFloatModel()),
              ChangeNotifierProvider.value(value: MineMsgProvider()), //消息中心红点，新人倒计时
              ChangeNotifierProvider.value(value: searchTextController), // 只针对视频搜索主页使用
              ChangeNotifierProvider.value(value: LimitTimeEventProvider()), //限时活动
            ],
            child: GetMaterialApp(
              navigatorKey: FlutterBase.navigatorKey,
              navigatorObservers: [routeObserver],
              debugShowCheckedModeBanner: false, //去掉右上角的debug字样
              initialRoute: SplashPage.routeName, //初始化的时候加载的路由
              getPages: AppRoutes.routePages,

              theme: ThemeData(
                platform: TargetPlatform.iOS,
                scaffoldBackgroundColor: Colors.black,
                primaryColor: AppColors.primaryColor,
                unselectedWidgetColor: Color(0xffD8D8D8),
                highlightColor: Colors.transparent,
                splashColor: Colors.transparent,
                tabBarTheme: const TabBarTheme(
                  dividerColor: Colors.transparent,
                  splashFactory: NoSplash.splashFactory,
                ),
                textSelectionTheme: TextSelectionThemeData(
                  cursorColor: AppColors.primaryHighColor,
                ),
                appBarTheme: AppBarTheme(
                  centerTitle: true,
                  iconTheme: IconThemeData(color: Colors.white, size: 18),
                  shadowColor: Colors.transparent,
                  elevation: 0,
                  scrolledUnderElevation: 0.0,
                  backgroundColor: Colors.black,
                  titleTextStyle: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w600),
                  systemOverlayStyle: SystemUiOverlayStyle.light,
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
